package org.example.educenter.controller;


import org.example.ResultCode.JwtUtils;
import org.example.ResultCode.R;
import org.example.ResultCode.ordervo.UcenterMemberOrder;
import org.example.educenter.entity.UcenterMember;
import org.example.educenter.entity.vo.RegisterVo;
import org.example.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lining
 * @since 2023-11-05
 */
@CrossOrigin
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;
    @PostMapping("login")//ba名字和密码封装成taken
    public R loginUser(@RequestBody UcenterMember member){
        String token=memberService.login(member);
        return R.ok().data("token",token);
    }
    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }
    //根据taken 获取用户信息
    @GetMapping("getMemberInfo")
    public  R getMemberInfo(HttpServletRequest request){
        String memberId= JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member=memberService.getById(memberId);
        return R.ok().data("userInfo",member);

    }
    //根据课程id获取课程信息(该方法供远程调用使用)
    @PostMapping("getCourseInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member=memberService.getById(id);
        //把member对象里面的值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder=new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
    //查询某天注册人数(该方法供远程调用使用)
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }



}

